
from yacf.Frontend.CUDA.plyparser import PLYParser, Coord, ParseError
from yacf.Frontend.CUDA.cuda_lexer import CUDALexer
from yacf.Frontend.CUDA import cuda_ast
from yacf.Frontend.C99 import c99_ast as c_ast

from yacf.Frontend.C99.c99_parser import C99Parser

class CUDAParser(PLYParser):

#	def __init__( 
#		self, 
#		lex_optimize=False,
#        lextab='yacf.Frontend.C99.lextab',
#        yacc_optimize=False,
#        yacctab='yacf.Frontend.C99.yacctab',
#        yacc_debug=False, 
#        lexer_class = CUDALexer):
#    	#super(C99Parser, self).__init__(self, lex_optimize, lextab, yacc_optimize,yacctab, yacc_debug, lexer_class)
#	    #super(C99Parser, self).__init__(self) 
#		C99Parser.__init__(self, lex_optimize, lextab, yacc_optimize,yacctab, yacc_debug, lexer_class)
#
	def p_external_declaration_5(self, p):
		""" external_declaration    : cuda_kernel
		"""
		p[0] = p[1]


	def p_cuda_kernel(self, p):
		""" cuda_kernel : GLOBAL function_definition """
		p[0] = p[2]
		p[0] = c_ast.CUDAKernel(
                    name=p[2].decl.name,
                    type='global',
                    function = p[2],
                    coord=p[2].coord)


	def p_postfix_expression_7(self, p):
		""" postfix_expression  : postfix_expression LKLAUNCH primary_expression COMMA primary_expression RKLAUNCH LPAREN argument_expression_list RPAREN
        	                    | postfix_expression LKLAUNCH primary_expression COMMA primary_expression RKLAUNCH LPAREN  RPAREN
		"""
		p[0] = c_ast.CUDAKernelCall(p[1], p[3], p[5], p[8] if len(p) > 9 else None, p[1].coord)

 
	# Ruyk : Directives inside statements
	#def p_statement_1(self, p):
	#    """ statement : pp_directive
	#    """
	#    p[0] = p[1][0]


